package com.jshare.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.jshare.dao.ArticleDAO;
import com.jshare.dao.Impl.ArticleDaoImpl;
import com.jshare.entity.ArCategory;
import com.jshare.entity.ArComment;
import com.jshare.entity.Article;
import com.jshare.entity.Member;
import com.jshare.entity.PageBean;
import com.jshare.service.ArticleServive;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 陆意
 * @version 1.0
 */
public class ArticleAction extends ActionSupport {

	@Resource(name = "articleService")
	private ArticleServive articleService;
	private Article article;
	private ArCategory arc;
	// 存放全部文章
	private List<Article> list;
	// 虚拟一个用户
	private Member m = new Member();
	/*
	 * 分页用的数据
	 */
	private int page; // 第几页
	private PageBean pageBean; // 包含分布信息的bean
	/**
	 * 点击的文章的id
	 * 文章的全部评论
	 */
	private int articleId;
	private Set arComments;
	
	/**
	 * 发表评论的文章的id
	 */
	private int aid;
	private String comment;//发表的评论
	private String name;//评论的发表者
	private String email;//邮箱
	private String commenter;//作者
	/**
	 * ======================================================================================
	 */
	/**
	 * 发表评论
	 */
	public String putComment(){
		
		//把这篇文章从数据库中查出来
		Article a=this.articleService.findById(aid);
		
		//打印一下
		System.out.println("评论内容"+comment);
		System.out.println("评论者"+commenter);
		System.out.println("评论者"+email);
		
		try {
			
			//创建一个评论实例
			ArComment ar=new ArComment();
			
			
			ar.setArticle(a);
			ar.setComment(comment);
			ar.setCommenter(commenter);
			ar.setEmail(email);
			
			//打印
			System.out.println("文章id"+this.aid);
			this.articleService.putComment(ar, ar.getArticle().getId());
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return "putCommentSuc";
	}
	/**
	 * 添加文章
	 */
	public String addArticle() {

		
		/*// 往session放一个user 虚拟的
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		session.put("user", m);

		Member user = (Member) session.get("user");*/
		Member user = m;
		if (user == null) {
			// 没有登录页面
			return "noLoginError";
		}
		// 添加文章类剩余的属性
		article.setPostdate(new Timestamp(new Date().getTime()));
		article.setMember(user);
		article.setArCategory(arc);

		try {
			articleService.addArticle(getArticle());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "addSuccess";
	}

	/**
	 * 带分页功能的显示全部文章
	 * 
	 */
	public String showAll() {

		this.pageBean = articleService.queryForPage(2, page);
		return "findAllSuc";
		/*
		 * try { list = articleService.findAll(); if (list != null) {
		 * System.out.println("a" + list.get(0).getTitle()); return
		 * "findAllSuc"; } } catch (Exception e) { // TODO: handle exception }
		 * return null;
		 */
	}
	/**
	 * 
	 * 根据点击的链接显示每篇文章
	 */
	public String eachArticle(){
		
		
		article=this.articleService.findById(articleId);
		System.out.println("跳转之后得到id没"+articleId);
		arComments = article.getArComments();
		Iterator it = arComments.iterator();
		 while(it.hasNext()){
			 ArComment ar=(ArComment) it.next();
			  System.out.println("评论的内容"+ar.getComment());
		 }
		/*System.out.println("评论的大小"+arComments.size());
		System.out.println("文章标题"+article.getTitle());*/
		return "findEachArticleSuc";
	}
	public int getPage() {
		return page;
	}

	public void setPage(int page) { // 若URL中无此参数,会默认为第1页
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public ArticleServive getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleServive articleService) {
		this.articleService = articleService;
	}

	public Member getM() {
		return m;
	}

	public void setM(Member m) {
		this.m = m;
	}

	public Article getArticle() {
		return article;
	}

	public ArCategory getArc() {
		return arc;
	}

	public void setArc(ArCategory arc) {
		this.arc = arc;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public List<Article> getList() {
		return list;
	}

	public void setList(List<Article> list) {
		this.list = list;
	}

	public int getArticleId() {
		return articleId;
	}

	public void setArticleId(int articleId) {
		this.articleId = articleId;
	}

	public Set getArComments() {
		return arComments;
	}

	public void setArComments(Set arComments) {
		this.arComments = arComments;
	}

	public int getAid() {
		return aid;
	}

	public void setAid(int aid) {
		this.aid = aid;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getCommenter() {
		return commenter;
	}
	public void setCommenter(String commenter) {
		this.commenter = commenter;
	}

	
	

}
